#include "bound.h"

void cBoundingBox::Draw(LPDIRECT3DDEVICE9 lpDevice)
{
	float fXOffset = (m_vecMaxPt.x - m_vecMinPt.x)/2;
	float fYOffset = (m_vecMaxPt.y - m_vecMinPt.y)/2;
	float fZOffset = (m_vecMaxPt.z - m_vecMinPt.z)/2;
	fXOffset += fXOffset/20.0f;
	fYOffset += fYOffset/20.0f;
	fZOffset += fZOffset/20.0f;
	float arrVertex[] = {
		m_vecCenterPt.x-fXOffset,m_vecCenterPt.y+fYOffset,m_vecCenterPt.z-fZOffset,1.0f,0.0f,0.0f,
		m_vecCenterPt.x+fXOffset,m_vecCenterPt.y+fYOffset,m_vecCenterPt.z-fZOffset,1.0f,0.0f,0.0f,
		m_vecCenterPt.x-fXOffset,m_vecCenterPt.y-fYOffset,m_vecCenterPt.z-fZOffset,1.0f,0.0f,0.0f,
		m_vecCenterPt.x+fXOffset,m_vecCenterPt.y-fYOffset,m_vecCenterPt.z-fZOffset,1.0f,0.0f,0.0f,
		m_vecCenterPt.x-fXOffset,m_vecCenterPt.y+fYOffset,m_vecCenterPt.z+fZOffset,1.0f,0.0f,0.0f,
		m_vecCenterPt.x+fXOffset,m_vecCenterPt.y+fYOffset,m_vecCenterPt.z+fZOffset,1.0f,0.0f,0.0f,
		m_vecCenterPt.x-fXOffset,m_vecCenterPt.y-fYOffset,m_vecCenterPt.z+fZOffset,1.0f,0.0f,0.0f,
		m_vecCenterPt.x+fXOffset,m_vecCenterPt.y-fYOffset,m_vecCenterPt.z+fZOffset,1.0f,0.0f,0.0f
	};
	DWORD dwIndex[] = {
		0,1,1,3,3,2,2,0,
		4,5,5,7,7,6,6,4,
		0,4,1,5,2,6,3,7};
	lpDevice->SetFVF(BOUND_FVF);
	lpDevice->DrawIndexedPrimitiveUP(D3DPT_LINELIST,0,8,12,dwIndex,D3DFMT_INDEX32,arrVertex,6*sizeof(float));
}
void cBoundingBox::ComputeBounding(LPD3DXMESH pMesh)
{
	D3DXVECTOR3* pVec;
	pMesh->LockVertexBuffer(0,(void**)&pVec);
	D3DXComputeBoundingBox(&pVec[0],pMesh->GetNumVertices(),
		pMesh->GetNumBytesPerVertex(),&m_vecMinPt,&m_vecMaxPt);
	pMesh->UnlockVertexBuffer();
	m_vecCenterPt = (m_vecMaxPt + m_vecMinPt)/2;
}
void cBoundingBox::AddPoint(D3DXVECTOR3& v){
	if (v.x<m_vecMinPt.x) m_vecMinPt.x = v.x;
	if (v.x>m_vecMaxPt.x) m_vecMaxPt.x = v.x;
	if (v.y<m_vecMinPt.y) m_vecMinPt.y = v.y;
	if (v.y>m_vecMaxPt.y) m_vecMaxPt.y = v.y;
	if (v.z<m_vecMinPt.z) m_vecMinPt.z = v.z;
	if (v.z>m_vecMaxPt.z) m_vecMaxPt.z = v.z;
}